<template>
  <div 
    class="svg-container"
    :style="{
      width: sizeWithUnit,
      height: sizeWithUnit,
      color: color,
      display: 'inline-block',
      lineHeight: 0
    }"
    v-html="processedSvg"
  ></div>
</template>

<script>
export default {
  name: 'SvgIcon',
  props: {
    svg: {
      type: String,
      required: true
    },
    size: {
      type: [Number, String],
      default: 24
    },
    color: {
      type: String,
      default: 'currentColor'
    },
    removeOriginalColor: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    sizeWithUnit() {
      return typeof this.size === 'number' ? `${this.size}px` : this.size
    },
    processedSvg() {
      let svg = this.svg
      
      // 移除固定宽高
      svg = svg.replace(/(width|height)=["'][^"']*["']/g, '')
      
      // 处理颜色
      if (this.removeOriginalColor) {
        // 处理fill属性
        svg = svg.replace(/fill=["'][^"']*["']/g, 'fill="currentColor"')
        // 处理stroke属性
        svg = svg.replace(/stroke=["'][^"']*["']/g, 'stroke="currentColor"')
        // 处理style中的颜色
        svg = svg.replace(/style=["'][^"']*["']/g, style => 
          style.replace(/(fill|stroke|color):[^;"]*(;|")/g, '$1:currentColor$2')
        )
      }
      
      // 确保svg元素有viewBox属性
      if (!svg.includes('viewBox') && svg.includes('<svg')) {
        svg = svg.replace('<svg', '<svg viewBox="0 0 24 24"')
      }
      
      return svg
    }
  }
}
</script>

<style scoped>
.svg-container {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.svg-container ::v-deep svg {
  width: 100%;
  height: 100%;
  display: block;
}

.svg-container ::v-deep path {
  fill: currentColor;
  stroke: currentColor;
}
</style>